package com.iwhalecloud.bss.kite.cucc.service.batch.attr.processor;

import java.util.List;
import java.util.Objects;

import org.springframework.stereotype.Component;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.SymbolConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.IUnicomRegionService;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.UnicomRegion;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.UnicomRegionQry;
import com.iwhalecloud.bss.kite.cucc.common.util.KiteAssert;
import com.iwhalecloud.bss.kite.cucc.service.batch.attr.BatchAttrProcessor;
import com.iwhalecloud.bss.kite.cucc.service.batch.attr.BatchAttrUtil;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.dto.KiteAttrDTO;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.ztesoft.bss.common.exception.BssException;

import lombok.RequiredArgsConstructor;

/**
 * @author lyx
 * @version V1.0
 * @Description: 城市 属性处理
 *      MV:60000190、60000189、60000188
 * @date 2021/6/25
 * @Package com.iwhalecloud.bss.kite.cucc.service.batch.attr.processor
 */
@Component
@BatchAttrProcessor(fieldName = {"2_0000105"}, prodId = {"60000190", "60000189", "60000188", "10003665"})
@RequiredArgsConstructor
public class CityProcessor implements IBatchAttrProcessor  {

    private final IUnicomRegionService unicomRegionService;

    private final IOperateSceneInstService operateSceneInstService;

    private final IQuerySceneInstService querySceneInstService;

    @Override
    public void process(KiteAttrDTO attr, KiteInst inst, String sceneInstId, String custId) {
        String fieldName = attr.getFieldName();
        String value = attr.getValue();
        KiteAssert.notBlank(value, ErrorConsts.BUSI_COMMON_ERROR, "城市填写不合法！正确格式为：省/市/区");
        String[] fields = value.split(SymbolConsts.SLASH);
        KiteAssert.state(fields.length >= 3, ErrorConsts.BUSI_COMMON_ERROR, "城市填写不合法！正确格式为：省/市/区");

        String provinceName = fields[0];
        String cityName = fields[1];
        String districtName = fields[2];

        // 解析出省市县
        List<UnicomRegion> provinces = unicomRegionService.subRegionFromPlatform(new UnicomRegionQry());
        UnicomRegion province = Objects.requireNonNull(provinces).stream()
            .filter(unicomRegion -> KiteStringUtils.isEqual(unicomRegion.getRegionName(), provinceName)).findFirst()
            .orElseThrow(() -> new BssException("BatchAccept", ErrorConsts.BUSI_COMMON_ERROR.getCode(),
                "城市填写不合法！解析省份出错！省份不存在"));
        String provinceCode = province.getRegionCode();

        KiteSceneInst kiteSceneInst = querySceneInstService.getSceneInst(sceneInstId);
        // 跨域校验
        if (!KiteStringUtils.equals(provinceCode, kiteSceneInst.getProvinceNbr())) {
            ErrorConsts.BUSI_COMMON_ERROR.throwOut("批量业务不允许受理跨域业务，请重新录入省市区信息！");
        }

        List<UnicomRegion> cities = unicomRegionService.subRegionFromPlatform(new UnicomRegionQry(provinceCode, "20"));
        UnicomRegion city = Objects.requireNonNull(cities).stream()
            .filter(unicomRegion -> KiteStringUtils.isEqual(unicomRegion.getRegionName(), cityName)).findFirst()
            .orElseThrow(() -> new BssException("BatchAccept", ErrorConsts.BUSI_COMMON_ERROR.getCode(),
                "城市填写不合法！解析地市出错！正确格式为：省/市/区"));
        String cityCode = city.getRegionCode();

        List<UnicomRegion> districts = unicomRegionService.subRegionFromPlatform(new UnicomRegionQry(cityCode, "30"));
        UnicomRegion district = Objects.requireNonNull(districts).stream()
            .filter(unicomRegion -> BatchAttrUtil.matchDistrict(districtName, unicomRegion.getRegionName())).findFirst()
            .orElseThrow(() -> new BssException("BatchAccept", ErrorConsts.BUSI_COMMON_ERROR.getCode(),
                "城市填写不合法！解析区县出错！正确格式为：省/市/区"));
        String districtCode = district.getRegionCode();

        // 设置省、市、县
        operateSceneInstService
            .setAttrValue(sceneInstId, inst.getInstType(), inst.getInstId(), "2_0000107", provinceCode, provinceName);
        operateSceneInstService
            .setAttrValue(sceneInstId, inst.getInstType(), inst.getInstId(), "2_0000109", cityCode, cityName);
        operateSceneInstService
            .setAttrValue(sceneInstId, inst.getInstType(), inst.getInstId(), "2_0000111", districtCode, districtName);

        // 设置城市的省市县
        String firstAddr = KiteStringUtils.joinWith(SymbolConsts.COMMA, provinceCode, cityCode, districtCode);
        String firstAddrDesc = KiteStringUtils.joinWith(SymbolConsts.COMMA, provinceName, cityName, districtName);
        operateSceneInstService
            .setAttrValue(sceneInstId, inst.getInstType(), inst.getInstId(), fieldName, firstAddr, firstAddrDesc);
    }
}
